#include "string.h"
#include "Includes.h"
#include "APP_Bill.h"
#include "APP_Comm.h"

U8 BFNo[BFNO_LENGTH]={0};//交易流水号

BILL_MODE_ST bill_model={
    .billModeNo=0,
    .SER=0,
    .SSR=0,
    .PER=0,
    .PSR=0,
    .FER=0,
    .FSR=0,
    .VER=0,
    .VSR=0,
    .LCR=0,
    .timerate_map={0}
};
BILL_STATS_ST bill_stats[GUN_NUM_MAX]={0};

#if GUN_NUM_MAX==1
U8 BillTimerPara[GUN_NUM_MAX]={1};
#elif GUN_NUM_MAX==2
U8 BillTimerPara[GUN_NUM_MAX]={1,2};
#endif

U32 ChgedMoney;//已充金额
U16 g_usAcumChgTime=0;
U16 g_usRemainChgTime=0;
BILL_STATE_EN g_enBillState[GUN_NUM_MAX]={BILL_STATE_STOP};

//统计各个时段用电量
//igun_num:1,2.内部转换0,1
void BillCalcQOEStats(U8 igun_num)
{
    int i;
    U8 gun_num;
    
    if(igun_num>GUN_NUM_MAX)
        return;
    gun_num=igun_num-1;
    for(i=0;i<TIME_PERIODS_NUM;i++)
    {
        bill_stats[gun_num].TtlQOE+=bill_stats[gun_num].QOE_map[i];
        //尖
        if(bill_model.timerate_map[i]==BILL_PAY_TYPE_SHARP)
        {
            bill_stats[gun_num].SQOE+=bill_stats[gun_num].QOE_map[i];
        }
        else if(bill_model.timerate_map[i]==BILL_PAY_TYPE_PEEK)
        {
            bill_stats[gun_num].PQOE+=bill_stats[gun_num].QOE_map[i];
        }
        else if(bill_model.timerate_map[i]==BILL_PAY_TYPE_SHOULDER)
        {
            bill_stats[gun_num].FQOE+=bill_stats[gun_num].QOE_map[i];
        }
        else if(bill_model.timerate_map[i]==BILL_PAY_TYPE_OFFPEEK)
        {
            bill_stats[gun_num].VQOE+=bill_stats[gun_num].QOE_map[i];
        }
    }
}
//igun_num:1,2.内部转换0,1
void BillStart(U8 igun_num)
{
    U32 idx;
    U8 gun_num;

    
    if(igun_num>GUN_NUM_MAX)
        return;
    gun_num=igun_num-1;

    printf("===>BillStart\r\n");

    memset(&bill_stats[gun_num],0,sizeof(BILL_STATS_ST));

    g_enBillState[gun_num]=BILL_STATE_START;
    TimeGet(&bill_stats[gun_num].time_start);
    NormalElecInfoGet(1, &bill_stats[gun_num].elec_start_info);

    idx=BILL_IDX(bill_stats[gun_num].time_start.Hour,bill_stats[gun_num].time_start.Min);
    bill_stats[gun_num].QOE_map[idx]=bill_stats[gun_num].elec_start_info.consumption;
    bill_stats[gun_num].idx_start=idx;
    if(gun_num==0)
        TTimer_Start(APP_GUN1_BILL_TIMERID);
    else
        TTimer_Start(APP_GUN2_BILL_TIMERID);
}

//igun_num:1,2.内部转换0,1
void BillStop(U8 igun_num)
{
    U32 idx;
    U8 gun_num;
    CHG_RECORD_PARA_ST para;

    if(igun_num>GUN_NUM_MAX)
        return;    
    gun_num=igun_num-1;
    
    printf("===>BillStop\r\n");    
    g_enBillState[gun_num]=BILL_STATE_STOP;
    TimeGet(&bill_stats[gun_num].time_end);
    NormalElecInfoGet(1, &bill_stats[gun_num].elec_end_info);
    idx=BILL_IDX(bill_stats[gun_num].time_end.Hour,bill_stats[gun_num].time_end.Min);
    bill_stats[gun_num].QOE_map[idx]=bill_stats[gun_num].elec_end_info.consumption-bill_stats[gun_num].QOE_map[idx];
    bill_stats[gun_num].idx_stop=idx;
    if(gun_num==0)
        TTimer_Stop(APP_GUN1_BILL_TIMERID);
    else
        TTimer_Stop(APP_GUN2_BILL_TIMERID);
    BillCalcQOEStats(igun_num);

    if(CommFsmGet()!=COMM_FSM_READY)
    {
        //保存交易纪录
    }
    else
    {
        para.gun_num=igun_num;
        para.stats=&bill_stats[gun_num];
        CommChgRecord(&para);
    }
    
}

//gun_num:1,2.内部转换0,1
int BillTimerCallback(void *pParameter)
{
    ST_TIME time;
    meter_info_t info;
    static U32 lastIdx,idx;
    U8 gun_num=*(U8*)pParameter;

    if(gun_num>GUN_NUM_MAX)
        return 1;
    gun_num-=1;
    
    TimeGet(&time);
    idx=BILL_IDX(time.Hour,time.Min);
    printf("BillTimerCallback gun_num=%d, QOE_idx=%d\r\n",gun_num,idx);
    if(lastIdx!=idx){
        MeterInfoGet(1, &info);
        bill_stats[gun_num].QOE_map[idx]=info.u16Energy;
        bill_stats[gun_num].QOE_map[lastIdx]=info.u16Energy-bill_stats[gun_num].QOE_map[lastIdx];
    }
    return 0;
}

void BillInit(void)
{
    //for(int i=1;i<=GUN_NUM_MAX;i++)
    //    BillStart(i);
}

void BillLoop(void)
{
    static U8 gun_idx=0;
    
    if(g_enBillState[gun_idx%GUN_NUM_MAX]==BILL_STATE_START)
    {

    }
    else if(g_enBillState[gun_idx%GUN_NUM_MAX]==BILL_STATE_STOP)
    {

    }
    gun_idx++;
}


